package com.mao.action.warehouse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.warehouse.JsonVoWareHouseClass;
import com.mao.model.product.ProductClass;
import com.mao.model.warehouse.WareHouseClass;
import com.mao.service.warehouse.IWareHouseClassService;
import com.mao.util.ClassUtil;
import com.mao.util.SessionHelper;

public class WareHouseClassAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private IWareHouseClassService<WareHouseClass> wareHouseClassService;

	private List<JsonVoWareHouseClass> result;

	private WareHouseClass wc;

	private JsonVoWareHouseClass voWc;

	// 修改分类
	public String jsonUpdateWareHouseClass() {
		WareHouseClass updateWc = wareHouseClassService.getWareHouseClassById(wc.getId());
		updateWc.setName(wc.getName());
		updateWc.setDescription(wc.getDescription());
		wareHouseClassService.updateWareHouseClass(updateWc);
		if(wc.getParent().getId() != null){
			WareHouseClass parent = wareHouseClassService.getWareHouseClassById(wc.getParent().getId());
			if(wc.getParent().getId() == 0){
				updateWc.setParent(null);
			}else{
				updateWc.setParent(parent);
			}
		}else{
			updateWc.setParent(null);
		}
		wareHouseClassService.updateWareHouseClass(updateWc);
		
		updateWc.setTreePath(ClassUtil.getTreePath(updateWc));
		updateWc.setLevel(ClassUtil.getLevel(updateWc.getTreePath()));
		wareHouseClassService.updateWareHouseClass(updateWc);
		// 更新所有下级分类的TreePath
		ClassUtil.updateTreePathAndLevel(updateWc, wareHouseClassService);
		voWc = new JsonVoWareHouseClass();
		voWc.setId(wc.getId());
		voWc.setText(wc.getName());
		return SUCCESS;
	}

	// 新增一级分类
	public String jsonSaveWareHouseClass() {
		wc.setParent(null);
		wc.setStore(SessionHelper.getSessionUser().getStore());
		wareHouseClassService.saveWareHouseClass(wc);
		
		wc.setTreePath(ClassUtil.getTreePath(wc));
		wc.setLevel(ClassUtil.getLevel(wc.getTreePath()));
		wareHouseClassService.updateWareHouseClass(wc);
		voWc = new JsonVoWareHouseClass();
		voWc.setId(wc.getId());
		voWc.setText(wc.getName());
		return SUCCESS;
	}

	// 新增下级分类
	public String jsonSaveLowWareHouseClass() {
		long parentId = wc.getParent().getId();
		if (parentId != 0) {
			WareHouseClass parent = wareHouseClassService.getWareHouseClassById(parentId);
			wc.setParent(parent);
		}
		wc.setStore(SessionHelper.getSessionUser().getStore());
		wareHouseClassService.saveWareHouseClass(wc);
		
		wc.setTreePath(ClassUtil.getTreePath(wc));
		wc.setLevel(ClassUtil.getLevel(wc.getTreePath()));
		wareHouseClassService.updateWareHouseClass(wc);
		voWc = new JsonVoWareHouseClass();
		voWc.setId(wc.getId());
		voWc.setText(wc.getName());
		voWc.setParentId(wc.getParent().getId());
		
		return SUCCESS;
	}
		
	public String jsonGetAllWareHouseClass() {
		result = new ArrayList<JsonVoWareHouseClass>();
		List<WareHouseClass> wcs = null;
		Map<String, Object> attsMap = null;
		JsonVoWareHouseClass wpc = null;
		// 初始页面
		if (wc == null) {
			wcs = wareHouseClassService.getAllWareHouseClass(SessionHelper.getSessionUser().getStore().getId());
		} else {
			// 读取下级
			wc = wareHouseClassService.getWareHouseClassById(wc.getId());
			Set<WareHouseClass> wcsets = wc.getLowClass();
			wcs = new ArrayList<WareHouseClass>();
			Iterator<WareHouseClass> it = wcsets.iterator();
			while (it.hasNext()) {
				wcs.add(it.next());
			}
		}
		for (int i = 0; i < wcs.size(); i++) {
			attsMap = new HashMap<String, Object>();
			wpc = new JsonVoWareHouseClass();
			wc = wcs.get(i);
			wpc.setId(wc.getId());
			wpc.setText(wc.getName());
			wpc.setState("closed");
			if(wc.getLevel() != 1){
				attsMap.put("parentId", wc.getParent().getId());
				attsMap.put("parentName", wc.getParent().getName());
			}
			attsMap.put("description", wc.getDescription());
			attsMap.put("treepath", wc.getTreePath());
			wpc.setAttributes(attsMap);
			result.add(wpc);
		}
		return SUCCESS;
	}

	// 删除分类
	public String jsonDeleteWareHouseClassById() {
		WareHouseClass whClass = wareHouseClassService.getWareHouseClassById(wc.getId());
		whClass.setStatus(ProductClass.STATUS_DELETED);
		wc = wareHouseClassService.updateWareHouseClass(whClass);
		voWc = new JsonVoWareHouseClass();
		voWc.setId(wc.getId());
		voWc.setText(wc.getName());
		return SUCCESS;
	}

	public String execute() {
		return SUCCESS;
	}
	

	public List<JsonVoWareHouseClass> getResult() {
		return result;
	}

	public void setResult(List<JsonVoWareHouseClass> result) {
		this.result = result;
	}

	public IWareHouseClassService<WareHouseClass> getWareHouseClassService() {
		return wareHouseClassService;
	}

	public void setWareHouseClassService(
			IWareHouseClassService<WareHouseClass> wareHouseClassService) {
		this.wareHouseClassService = wareHouseClassService;
	}

	public WareHouseClass getWc() {
		return wc;
	}

	public void setWc(WareHouseClass wc) {
		this.wc = wc;
	}

	public JsonVoWareHouseClass getVoWc() {
		return voWc;
	}

	public void setVoWc(JsonVoWareHouseClass voWc) {
		this.voWc = voWc;
	}

}
